package com.jf.blog.controller;

import com.jf.blog.bean.Tag;
import com.jf.blog.form.tag.TagForm;
import com.jf.blog.form.tag.TagSearchForm;
import com.jf.blog.service.TagService;
import com.jf.blog.util.ResultJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author JF
 * @since 2025-06-20
 */
@RestController
@RequestMapping("/tag")
@Api(tags = "标签模块")
public class TagController {
    @Autowired
    private TagService tagService;

    @GetMapping("/page")
    @ApiOperation("分页筛选")
    public ResultJson page(@RequestParam(defaultValue = "1") @ApiParam(value = "当前页", required = true) int page, @RequestParam(defaultValue = "5") @ApiParam(value = "当前页", required = true) int limit, TagSearchForm searchForm) {
        return tagService.getPage(page, limit, searchForm);
    }

    @GetMapping("/info/{tagId}")
    @ApiOperation("查看详情")
    public ResultJson info(@PathVariable int tagId) {
        return ResultJson.success(tagService.getById(tagId));
    }

    @PostMapping("/update")
    @ApiOperation("添加或修改")
    public ResultJson update(@RequestBody TagForm tagForm) {
        Tag tag = new Tag();
        BeanUtils.copyProperties(tagForm, tag);
        tagService.saveOrUpdate(tag);
        return ResultJson.success();
    }

    @DeleteMapping("/delete")
    @ApiOperation("批量删除")
    public ResultJson delete(@RequestBody List<Integer> tagIds) {
        tagService.delete(tagIds);
        return ResultJson.success();
    }

    @GetMapping("/select")
    @ApiOperation("下拉框数据")
    public ResultJson select() {
        return tagService.select();
    }

}
